//
//  CouponDoorViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-11-19.
//
//

#import "CouponDoorViewController.h"
#import "UIImageView+OnlineImage.h"
#import "NetworkAlertView.h"
#import "CouponDetailViewController.h"

@interface CouponDoorViewController ()

@end

@implementation CouponDoorViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.shopInfos count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil];
    
    Shop *shopinfo = [self.shopInfos objectAtIndex:indexPath.row];
    
    //加载图像
    UIImageView *imgview  = [[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 70, 90)]autorelease];
    [imgview setOnlineImage:self.imgurl];
    [cell.contentView addSubview:imgview];
    
    //加载名称
    UILabel *namelbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 200, 40)]autorelease];
    namelbl.text = shopinfo.name;
    namelbl.font = [UIFont systemFontOfSize:15];
    namelbl.textColor = [UIColor redColor];
    namelbl.adjustsFontSizeToFitWidth = YES;
    [cell.contentView addSubview:namelbl];
    
    //加载描述
    UILabel *desclbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 50, 200, 40)]autorelease];
    desclbl.text = shopinfo.address;
    desclbl.font = [UIFont systemFontOfSize:10];
    desclbl.lineBreakMode = UILineBreakModeWordWrap;
    desclbl.numberOfLines = 0;
    [cell.contentView addSubview:desclbl];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Shop *shopinfo = [self.shopInfos objectAtIndex:indexPath.row];

    //等待网络
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking showNetView];
    
    //请求数据
    CouponManager *couponmanager = [CouponManager shareManager];
    couponmanager.delegate = self;
    [couponmanager getCoupons:shopinfo.shopId];
}

#pragma network to next
- (void)getCouponsFinish:(BOOL)isSuccess
             couponArray:(NSMutableArray*)couponArray
{
    //取消等待
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking delView];
    
    CouponDetailViewController *detailController = [[CouponDetailViewController alloc] init];
    detailController.couponArray = couponArray;
    detailController.shopInfos =  self.shopInfos;
    [self.navigationController pushViewController:detailController animated:YES];
    [detailController release];
}

@end


